home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / LayerGroups / MainMenu.cp < prev    next >
Encoding:
Text File  |  1998-10-23  |  5.1 KB  |  259 lines  |  [TEXT/CWIE]

  1. /* MainMenu.cp */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <LowMem.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <ToolUtils.h>
  13.  
  14. #include "Globals.h"
  15. #include "ResourceDefs.h"
  16. #include "Miscellany.h"
  17. #include "AMApp.h"
  18. #include "AMDoc.h"
  19. #include "AMEngine.h"
  20. #include "AMWindow.h"
  21. #include "MainMenu.h"
  22.  
  23.  
  24. /*----------*/
  25. static long        GetCommandFromMenu (long        menuChoice);
  26. static void        DoApple            (short            itemNr);
  27. static void        Enable            (short            itemNr,
  28.                                  Boolean        enabled);
  29. static void        EnableTitle        (MenuHandle        menu,
  30.                                  Boolean        enabled);
  31.  
  32. /*----------*/
  33. void    InitTitles (void)
  34. {
  35. } /*InitTitles*/
  36.  
  37. /*----------*/
  38. void    LoadMenus (void)
  39. {
  40.     AppleMenu    = GetMenu (MENU_Apple);
  41.     FailNilResource ((Handle)AppleMenu);
  42.     AppendResMenu (AppleMenu, 'DRVR');
  43.     FileMenu    = GetMenu (MENU_File);
  44.     EditMenu    = GetMenu (MENU_Edit);
  45.     GroupBoxMenu    = GetMenu (MENU_GroupBox);
  46.     MoreMenu    = GetMenu (MENU_More);
  47.  
  48.     InsertMenu (AppleMenu, 0);
  49.     InsertMenu (FileMenu, 0);
  50.     InsertMenu (EditMenu, 0);
  51.     InsertMenu (MoreMenu, 0);
  52.     InsertMenu (GroupBoxMenu, -1);
  53.  
  54.     DrawMenuBar ();
  55. } /*LoadMenus*/
  56.  
  57. //----------
  58. long    GetCommandFromMenu (
  59.     long        menuChoice)
  60. {
  61.     long        commandID = 0;
  62.  
  63.     switch (menuChoice) {
  64.         case cAppleAbout:
  65.                 commandID = cmdInvokeAbout;
  66.             break;
  67.         case cFileNew:
  68.                 commandID = cmdNew;
  69.             break;
  70.         case cFileOpen:
  71.                 commandID = cmdOpen;
  72.             break;
  73.         case cFileClose:
  74.                 commandID = cmdClose;
  75.             break;
  76.         case cFileSave:
  77.                 commandID = cmdSave;
  78.             break;
  79.         case cFileSaveAs:
  80.                 commandID = cmdSaveAs;
  81.             break;
  82.         case cFileRevert:
  83.                 commandID = cmdRevert;
  84.             break;
  85.         case cFilePageSetup:
  86.                 commandID = cmdPageSetup;
  87.             break;
  88.         case cFilePrint:
  89.                 commandID = cmdPrint;
  90.             break;
  91.         case cFileQuit:
  92.                 commandID = cmdQuit;
  93.             break;
  94.         case cEditUndo:
  95.                 commandID = cmdUndo;
  96.             break;
  97.         case cEditCut:
  98.                 commandID = cmdCut;
  99.             break;
  100.         case cEditCopy:
  101.                 commandID = cmdCopy;
  102.             break;
  103.         case cEditPaste:
  104.                 commandID = cmdPaste;
  105.             break;
  106.         case cEditClear:
  107.                 commandID = cmdClear;
  108.             break;
  109.         case cEditSelectAll:
  110.                 commandID = cmdSelectAll;
  111.             break;
  112.         case cEditShowClipboard:
  113.                 commandID = cmdShowClipboard;
  114.             break;
  115.         case cMoreEditFields:
  116.                 commandID = cmdShowEditFields;
  117.             break;
  118.  
  119.         default:
  120.                 commandID = -menuChoice;
  121.     }
  122.  
  123.     return commandID;
  124. }
  125.  
  126. //----------
  127. void    DoApple (
  128.     short        itemNr)
  129. {
  130.     Str255            name;
  131.     short            refNum;
  132.  
  133.     GetMenuItemText (AppleMenu, itemNr, name);
  134.     refNum = OpenDeskAcc (name);
  135. }
  136.  
  137. /*----------*/
  138. void    DoMenu (
  139.     long        menuChoice)
  140. {
  141.     long            commandID;
  142.     AMDoc*            curDoc;
  143.     short            menuID;
  144.     short            itemNr;
  145.  
  146.     commandID = GetCommandFromMenu (menuChoice);
  147.     curDoc = cur->mDoc;
  148.     if (cur->DoCommand (commandID)) {
  149.         // cur window handled it
  150.     } else if ((curDoc != nil)
  151.     && (curDoc->DoCommand (commandID))) {
  152.         // document handled it
  153.     } else if (gApplication->DoCommand (commandID)) {
  154.         // application handled it
  155.     } else {
  156.         menuID = HiWord (menuChoice);
  157.         itemNr = LoWord (menuChoice);
  158.         if (menuID == MENU_Apple) {
  159.             DoApple (itemNr);
  160.         }
  161.     }
  162.  
  163.     HiliteMenu (0);
  164. } /*DoMenu*/
  165.  
  166. /*----------*/
  167. MenuHandle        menu;
  168. Boolean            menuBarChanged;
  169.  
  170. /*----------*/
  171. static void Enable    (short        itemNr,
  172.                      Boolean    enabled)
  173. {
  174.     if (enabled) {
  175.         EnableItem  (menu, itemNr);
  176.     } else {
  177.         DisableItem (menu, itemNr);
  178.     }
  179. } /*Enable*/
  180.  
  181. /*----------*/
  182. static void EnableTitle    (MenuHandle        menu,
  183.                          Boolean        enabled)
  184. {
  185.     if (enabled != ((**menu).enableFlags & 1)) {
  186.         menuBarChanged = true;
  187.     }
  188.     if (enabled) {
  189.         EnableItem  (menu, 0);
  190.     } else {
  191.         DisableItem (menu, 0);
  192.     }
  193. } /*EnableTitle*/
  194.  
  195. /*----------*/
  196. void    UpdateMenus (void)
  197. {
  198.     WindowPeek        frontPeek;
  199.     Boolean            isFront;        /*is there a front window?*/
  200.     Boolean            isCur;            /*is there a current window?*/
  201.     Boolean            isCurDoc;        /*is there a current document?*/
  202.     Boolean            isDirty;        /*is it dirty?*/
  203.     Boolean            hasFile;        /*does it have a file?*/
  204.     Boolean            isSelected;        /*is anything selected?*/
  205.     Boolean            isDesk;            /*is the front window a desk acc?*/
  206.     Boolean            isText;            /*is there a current text field?*/
  207.     Boolean            isScrap;        /*is there any scrap?*/
  208.     menuBarChanged = false;
  209.  
  210.     isFront        = (FrontWindow () != nil);
  211.     isCur        = (curWindow != nil);
  212.     isDirty        = false;
  213.     hasFile        = false;
  214.     isSelected    = false;
  215.     isCurDoc    = (cur->mDoc != nil);
  216.     if (isCurDoc) {
  217.         isDirty        = cur->mDoc->mEngine->IsDirty ();
  218.         hasFile        = cur->mDoc->mEngine->HasFile ();
  219.     }
  220.  
  221.     isDesk = false;
  222.     if (isFront) {
  223.         frontPeek    = (WindowPeek) FrontWindow ();
  224.         isDesk        = (frontPeek->windowKind < 0);
  225.     }
  226.  
  227.     TEHandle    curTE = cur->GetCurTE ();
  228.  
  229.     isText        = (curTE != nil);
  230.     isScrap        = false;
  231.     if (isText) {
  232.         isSelected    = ((**curTE).selStart != (**curTE).selEnd);
  233.         isScrap        = (TEGetScrapLength () > 0);
  234.     }
  235.  
  236.     menu = FileMenu;
  237.     Enable (cFileClose,        isFront);
  238.     Enable (cFileSave,        isDirty);
  239.     Enable (cFileSaveAs,    isCur);
  240.     Enable (cFileRevert,    isDirty);
  241.  
  242.     menu = EditMenu;
  243.     if (isFront) {
  244.         Enable (cEditUndo,        isDesk);
  245.         Enable (cEditCut,        isDesk || isSelected);
  246.         Enable (cEditCopy,        isDesk || isSelected);
  247.         Enable (cEditPaste,        isDesk || isScrap);
  248.         Enable (cEditClear,        isDesk || isSelected);
  249.         Enable (cEditSelectAll,    isText);
  250.  
  251.     }
  252.     EnableTitle (EditMenu,     isFront);
  253.  
  254.  
  255.     if (menuBarChanged) {
  256.         DrawMenuBar ();
  257.     }
  258. } /*UpdateMenus*/
  259.